<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户数据表格</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <style>
        .search-box {
            margin-bottom: 15px;
        }

        .layui-form-item {
            margin-bottom: 0;
        }
    </style>
</head>
<body>
<div class="layui-card">
    <div class="layui-card-header">用户数据表</div>
    <div class="layui-card-body">
        <!-- 搜索框 -->
        <div class="search-box layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <select id="searchKey" class="layui-input">
                        <option value="username">用户名</option>
                        <option value="email">邮箱</option>
                        <option value="role">角色</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <input type="text" id="searchValue" placeholder="输入搜索内容" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="searchBtn">搜索</button>
                </div>
            </div>
        </div>

        <!-- 数据表格 -->
        <table id="userTable" lay-filter="userTable"></table>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js"></script>
<script>
    layui.use(['table', 'jquery'], function () {
        var table = layui.table;
        var $ = layui.$;

        // 初始化表格
        var tableIns = table.render({
            elem: '#userTable',
            url: '/fspl/api/users',
            page: true,
            toolbar: true,
            cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true},
                {field: 'username', title: '用户名', width: 150},
                {field: 'email', title: '邮箱', width: 200},
                {field: 'role', title: '角色', width: 120, sort: true},
                {title: '操作', width: 150, align: 'center', toolbar: '#actionBar'}
            ]],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data
                };
            }
        });

        // 搜索按钮事件
        $('#searchBtn').click(function () {
            var searchKey = $('#searchKey').val();
            var searchValue = $('#searchValue').val();

            // 重载表格数据
            tableIns.reload({
                where: {
                    searchKey: searchKey,
                    searchValue: searchValue
                },
                page: {curr: 1}
            });
        });

        // 监听排序事件
        table.on('sort(userTable)', function (obj) {
            tableIns.reload({
                initSort: obj,
                where: {
                    field: obj.field,
                    order: obj.type
                }
            });
        });
    });
</script>

<!-- 操作列模板 -->
<script type="text/html" id="actionBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>